package com.asdhawaii.otello.players;

import static org.junit.Assert.assertTrue;
import org.junit.Test;
import com.asdhawaii.otello.OtelloPanel;
import com.asdhawaii.otello.board.SquareContents;

/**
 * Compares the performance of the Otello AI against each other.
 * 
 * @author Branden Ogata
 *
 */

public class AiComparison
{
  /**
   * Tests Level1Ai.
   * 
   */
  
  @Test
  public void testLevel1Ai()
  {
    // Level1Ai vs. Level0Ai
    int level1Wins = 0;
    int level0Wins = 0;
    final OtelloPanel panel = new OtelloPanel(new Level1Ai(SquareContents.BLACK), 
                                              new Level0Ai(SquareContents.WHITE));
    
    for (int i = 0; i < 8; i++)
    {
      // This runs through an entire game of Otello
      Thread t = new Thread(new Runnable() 
      {
        @Override
        public void run()
        {
          panel.reset();
          SquareContents toMove = SquareContents.WHITE;
          
          while (panel.getWinner() == null)
          {
            panel.nextMove(toMove);
            toMove = toMove.opposite();
          }        
        }
      });
      
      try
      {
        t.start();
        t.join();
        
        if (panel.getWinner().equals(SquareContents.BLACK))
        {
          System.out.println("Black won");
          level1Wins++;
        }
        else if (panel.getWinner().equals(SquareContents.WHITE))
        {
          System.out.println("White won");
          level0Wins++;
        }
        else
        {
          System.out.println("Draw");
        }
      }
      catch (InterruptedException e)
      {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }
    
    assertTrue("Level1Ai should defeat Level0Ai", 
               level1Wins > level0Wins);

  }

}
